<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ItemGood</title>

</head>
<body>
<div id="toolbar">
    <a class="btn" href="javascript:;" onclick=""><i class="fa fa-plus"></i> 添加商品</a>
    <a class="btn" href="javascript:;" onclick=""><i class="fa fa-edit"></i> 编辑商品</a>
    <a class="btn" href="javascript:;" onclick=""><i class="fa fa-trash-o"></i> 删除商品</a>
</div>
<table id="table"></table>


<script src="../../plugins/boostrap-table/bootstrap-table.js" ></script>
<script src="/dist/layer/layer.js" ></script>

<!-- Latest compiled and minified CSS -->
<!--<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.min.css">-->

<!-- Latest compiled and minified JavaScript -->
<!--<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.min.js"></script>-->

<script src="../../plugins/boostrap-table/locale/bootstrap-table-zh-CN.js" ></script>
<script type="text/javascript">

    var $table = $('#table');

    var goodevents = {
        'click .upgood':function(e,value,row,index){
            $.post('/good/shelves/'+ row.goodId);
            $table.bootstrapTable('refresh');
        },
        'click .downgood':function(e,value,row,index){
            $.post('/good/down/'+ row.goodId);
            $table.bootstrapTable('refresh');
        },
        'click .recommend':function(e,value,row,index){
            $.post('/good/recommend/'+ row.goodId);
            $table.bootstrapTable('refresh');
        },
        'click .unrecommend':function(e,value,row,index){
            $.post('/good/unrecommend/'+ row.goodId);
            $table.bootstrapTable('refresh');
        }
    }

    $table.bootstrapTable({
        url: '../../good/l?id=1',
//        height: 500,
        method: 'POST',
        striped: true,
        height:getheight(),
        showRefresh: true,
        minimumCountColumns: 2,
        clickToSelect: false,
        pagination: true,
        paginationLoop: false,
        sidePagination: 'server',
        queryParams:queryParams,
        smartDisplay: true,
        idField: 'goodid',
        sortName: 'goodid',
        sortOrder: 'desc',
        toolbar: '#toolbar',
        columns: [
            {field: 'state', checkbox: true,valign:"middle"},
            {field: 'goodName', title: '商品名称', sortable: true, halign: '', valign:"middle"},
            {field: 'goodPrice', title: '商品价格', sortable: true, halign: '',valign:"middle"},
            {field: 'goodNuit', title: '单位', sortable: true, halign: '',valign:"middle"},
            {field: 'goodPic', title: '图片', sortable: true, halign: '',valign:"middle",formatter:function(value,row,index){
                return "<img src='"+value+"' />";
            }},
            {field: 'goodStatus', title: '状态', sortable: true, halign: '',valign:"middle",formatter:function (value,row,index){
                if(value == 1){
                    return '正常'
                }else if(value ==2){
                    return  '上架'
                }else{
                    return '未知状态'
                }
            }},
            {field: 'recommend', title: '推荐', sortable: true, halign: '',valign:"middle",formatter:function (value,row,index) {
                if(value== 1){
                    return "已推荐";
                }else{
                    return "未推荐";
                }
            }},
            {field: '', title: '操作', halign: 'center',valign:"middle",
                formatter:function(value,row,index){
                const upbutton = "<a href='javascript:void(0)' class='btn btn-info btn-sm margin-r-5 upgood'>上架</a>";
                const downbutton = "<a href='javascript:void(0)' class='btn btn-danger btn-sm margin-r-5 downgood'>下架</a>";
                const recommend = "<a href='javascript:void(0)' class='btn btn-info btn-sm margin-r-5 recommend'>推荐</a>";
                const unrecommend = "<a href='javascript:void(0)' class='btn btn-danger btn-sm margin-r-5 unrecommend'>取消推荐</a>";
                console.info(row.goodStatus == '1' ? upbutton : downbutton )
                return "<span>" + (row.goodStatus == 1 ? upbutton : downbutton) + (row.recommend == 1 ? unrecommend : recommend)
                    "<a href='javascript:void(0)' class='btn btn-info btn-sm margin-r-5' >修改</a></span>"
            },events:goodevents}

        ]
    });


    /**查询条件与分页数据 */
    function queryParams(pageReqeust) {
        console.info(pageReqeust);
//        pageReqeust.pageNo = this.offset;
//        pageReqeust.pageSize = this.pageNumber;
//        return pageReqeust;

        var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            limit : pageReqeust.limit,   //页面大小
            offset : pageReqeust.offset,  //页码
            sort : pageReqeust.sort,
            order : pageReqeust.order,
            object: [],
            //statu: $("#txt_search_statu").val()
        };
        return JSON.stringify(temp);
    }


    function deletemethod(){
        var select = $table.bootstrapTable('getSelections');
        if(select.length == 1){
            layer.msg("成功删除")


            //$table.bootstrapTable("remove",{field: 'id', values:  select[0].id});


            //$.get('/item/d/' + select[0].id);
            $table.bootstrapTable('refresh');
        }else {
            layer.alert("请选择一条记录删除",{"icon": 2  })
        }

    }



    function getheight(){
//        var tableheight = $("div.content-wrapper").height();
        var tableheight = $(window).height() - 287;
//        var tableheight = $(window).height() - $(".main-header").height() - $('.content-header').height() - $(".main-footer").height();
        return tableheight;
    }

</script>
</body>
</html>